﻿/*
 * copyright (c) 2009 hvbird
 *
 * This file is part of tdlib.
 *
 * tdlib is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * tdlib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TDLib.Export
{
    public class Excel
    {
#if NET_2_0
        /// <summary>
        /// 输出Excel
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="oDS"></param>
        /// <param name="http"></param>
        public static void ExportToClient<T>(T oDS, HttpContext http,string filename)
        {
            http.Response.Clear();
            http.Response.Charset = "gb2312;";
            http.Response.ContentType = "application/vnd.ms-excel";
            http.Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename);
            System.IO.StringWriter oSW = new System.IO.StringWriter();
            HtmlTextWriter oHW = new HtmlTextWriter(oSW);
            DataGrid oDG = new DataGrid();
            oDG.DataSource = oDS;
            oDG.DataBind();
            oDG.RenderControl(oHW);
            http.Response.Write(oSW.ToString().Replace("border=\"1\"",""));
            http.Response.Flush();
            http.Response.Close();
        }
#endif

#if NET_1_1
        public static void ExportExcel(IEnumerable oDS,HttpContext http)
        {
            http.Response.Clear();
            http.Response.Charset = "gb2312;";
            http.Response.ContentType = "application/vnd.ms-excel";
            System.IO.StringWriter oSW = new System.IO.StringWriter();
            HtmlTextWriter oHW = new HtmlTextWriter(oSW);
            DataGrid oDG = new DataGrid();
            oDG.DataSource = oDS;
            oDG.DataBind();
            oDG.RenderControl(oHW);
            http.Response.Write(oSW.ToString());
            http.Response.Flush();
            http.Response.Close(); 

        }
#endif

    }
}
